const api = require("../../server/Api.js");
const util = require("../../utils/util.js");
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shop: {},
    jl: "",
    fromType: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let id = options.id
    if (options.type) {
      this.setData({
        fromType: options.type
      })
    }
    this.getShopInfo(id);
  },

  getShopInfo: function(id) {
    const that = this;
    api.getShopInfo(id || app.globalData.shopId).then(res => {
      console.log("店铺信息:", res);
      //请求成功
      res.fans = res.fans || "0"
      res.buyExponent = app.setBuyExponent(res.buyExponent)
      if (res.images && res.images !== "") {
        res.images = app.resetImageUrl(res.images, 200, 200)
      } else {
        res.images = "../../images/logo1.png";
      }
      that.setData({
        shop: res
      })
      app.globalData.qqmapsdk.calculateDistance({
        mode: "straight",
        to: res.latitude + "," + res.longitude,
        success: function(res) {
          if (res.result.elements.length > 0) {
            that.setData({
              jl: app.setMtoKm(res.result.elements[0].distance)
            })
          }
        },
        fail: function(error) {
          console.error("获取距离失败", error);
        },
      });
    }).catch(msg => {
      let message = typeof msg == "string" ? msg : msg.message;
      app.showToast(message);
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  makePhone: function() {
    let that = this;
    if (that.data.shop.mobile) {
      app.makePhoneCall(that.data.shop.mobile)
    } else {
      app.showToast("未配置号码")
    }
  },
  toSetPosition() {
    let that = this;
    tt.showModal({
      title: '店铺定位',
      content: '确认把您目前所在的位置更新为您的店铺定位位置吗？',
      success: function(e) {
        if (e.confirm) {
          tt.getLocation({
            type: 'gcj02',
            success: res => {
              let storeDtos = [{
                id: app.globalData.userShopId[0].id,
                latitude: res.latitude,
                longitude: res.longitude
              }]
              api.batchUpdateStores(storeDtos).then(res => {
                tt.showToast('更新店铺定位成功');
              }).catch(res => {
                app.showToast('更新店铺定位失败');
              });
            }
          })
        }
      }
    })
  }
})